<?php
class StickyIndexController  extends Zone_Action{
	public function indexAction(){
		$user_id = get_user_id();
		$posts = $this->Model->fetchAll("SELECT *,CAST(`date` as DATE) as `date` FROM `stickies` WHERE `user_id`='$user_id'");
		return $this->setContent(json_encode($posts));
	}
	
	public function addAction(){
		if( isPost() ){
			$data = array(
				user_id => get_user_id()	
			);
			$this->Model
				->insert("stickies",$data);		
		}
		$this->setContent( $this->Model->lastId());
		$this->removeLayout();	
	}

	public function editAction(){
		if( isPost() ){
			$data = array(
				content	=> get('content'),
				date	=> date('Y-m-d H:i:s'),
				width	=> get('mW'),	
				height	=> get('mH'),
				x		=> get('dx'),
				y		=> get('dy'),
				user_id => get_user_id()	
			);
			$this->Model
				->update("stickies",$data,"`ID`='".get('ID',0)."' AND `user_id`='".get_user_id()."'");		
		}
		$this->setContent('');
		$this->removeLayout();	
	}

	public function deleteAction(){
		$this->Model->delete("stickies","`ID`='".get('ID',0)."' AND `user_id`='".get_user_id()."'");		
	}
}	